angular.module('moti.controllers.inspectorslist', ['ng-sortable', 'moti.services.regionsService', 'moti.services.inspectorsService', 'moti.directive.modal'])
    .controller('inspectorsListCtrl', ['$scope', 'RegionsInterface', 'InspectorsInterface', '$modal', function($scope, RegionsInterface, InspectorsInterface, $modal){

        //获取景区列表
        RegionsInterface.getRegionsList().then(function(data){
            $scope.regions = data;
            //获取检票员列表
            InspectorsInterface.getInspectorsList().then(function(data){
                $scope.inspectors = data;
                //根据景区id获取景区名称
                angular.forEach($scope.inspectors, function(value, key) {
                    value.region_desc = _.result(_.find($scope.regions, function(chr) {
                        return chr.region_id == value.region_id;
                    }), 'name');
                    value.create_time = moment(value.create_time).format('YYYY-MM-DD');
                });
            });
        });

        //删除检票员
        var dalete_modal = $modal({
            id : 'delete-modal',
            text : '确认要删除此检票员吗？该操作不可恢复！'
        });
        $scope.deleteInspector = function(inspector){
            dalete_modal.show();
            $scope.curInspectorId = inspector.inspector_id;
        };
        $("#delete-btn").off("click").on("click", function(){
            InspectorsInterface.deleteInspector($scope.curInspectorId).then(function(data){
                dalete_modal.hide();
                $modal({id : 'tip-modal', text : "删除检票员成功"}).show().autoHide();
                _.remove($scope.inspectors, function(n){
                    return $scope.curInspectorId == n.inspector_id;
                });
            });
        });

    }]);